package com.china08.yxyapi.api.comm;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBookContents;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookContentsRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class BookContentController extends YxyApiBaseController {

    @Autowired
    private YxyResBookContentsRepository yrbcr;

    @ApiOperation(value = "获得目录")
    @RequestMapping(value = "/api_yw/comm/bookcontent", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(@RequestBody ReqModel reqModel) {
        RespModel model = new RespModel();
        List<YxyResBookContents> listcoContents = yrbcr.findByBookIdAndIsViewOrderByOrderNum(reqModel.book, 1);
        List<BookContentRespModel> list = new ArrayList<BookContentController.BookContentRespModel>();
        for (YxyResBookContents yxyResBookContents : listcoContents) {
            BookContentRespModel bookContentRespModel = new BookContentRespModel();
            bookContentRespModel.id = yxyResBookContents.getId();
            bookContentRespModel.name = yxyResBookContents.getName();
            bookContentRespModel.pId = yxyResBookContents.getpId();
            list.add(bookContentRespModel);
        }
        model.bookContent = list;
        model.setMessage("成功");
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String book;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(book, null), "请填写课本ID");
        }

    }

    protected static class BookContentRespModel {
        public String id;
        public String pId;
        public String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getpId() {
            return pId;
        }

        public void setpId(String pId) {
            this.pId = pId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }

    protected static class RespModel extends YxyApiRespModel {

        public List<BookContentRespModel> bookContent;

        public List<BookContentRespModel> getBookContent() {
            return bookContent;
        }

        public void setBookContent(List<BookContentRespModel> bookContent) {
            this.bookContent = bookContent;
        }

    }

}
